<template>
    <div>
        <a-form layout="inline"
                :model="searchData"
                style="margin: 10px 0">
            <a-form-item>
                <a-input v-model:value="searchData.specName" placeholder="规格名称"></a-input>
            </a-form-item>
            <a-form-item>
                <a-button type="primary" @click="load">搜索</a-button>
            </a-form-item>
            <a-form-item>
                <a-button type="default" @click="rest">重置</a-button>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-bottom: 10px" @click="openModal({})">新增</a-button>
        <a-table :dataSource="specificationList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 bordered>
            <template v-slot:action="{text,record}">
                <a-button type="primary" @click="openModal(record)">编辑</a-button>
                <a-popconfirm
                        title="删除数据无法恢复，请确认是否删除？"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="okDelete(record.id)"
                >
                    <a-button type="danger">删除</a-button>
                </a-popconfirm>
            </template>
        </a-table>
        <a-modal v-model:visible="visible" title="新增或编辑" @ok="handleOk">
            <a-form v-model="specification"
                    :label-col="{ span: 8 }">
                <a-form-item label="规格名称">
                    <a-input v-model:value="specificationPojo.specification.specName"></a-input>
                </a-form-item>
                <a-form-item label="规格选项">
                    <a-button type="primary" @click="addSpecOption">新增规格选项</a-button>
                    <table style="width: 100%" border="1px">
                        <thead>
                        <tr>
                            <td>规格选项</td>
                            <td>排序</td>
                            <td>操作</td>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="(specOption,index) in specificationPojo.specificationOptionList" :key="specOption.id">
                            <td>
                                <a-input v-model:value="specOption.optionName"></a-input>
                            </td>
                            <td>
                                <a-input v-model:value="specOption.orders"></a-input>
                            </td>
                            <td>
                                <a-button type="danger" @click="delSpecOption(index)">删除</a-button>
                            </td>
                        </tr>
                        </tbody>

                    </table>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue'

    export default {
        name: "admin-specification",
        setup() {
            const columns = [
                {
                    title: '规格ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '规格名称',
                    dataIndex: 'specName',
                    key: 'specName',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]

            const specification = ref({})
            const searchData = ref({})
            const specificationList = ref()
            const specificationPojo = ref({
                specification: {},
                specificationOptionList: []
            })
            const pagination = ref({
                current: 1,
                pageSize: 10,
            })
            const visible = ref(false);

            const load = () => {
                axios.get('/mall-manager/specification/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        specName: searchData.value.specName
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    specificationList.value = rs.data.data.list
                    pagination.value.total = rs.data.data.total
                })
            }

            const handlerChange = (pageInfo) => {
                pagination.value = pageInfo;
                load()
            }

            const rest = () => {
                searchData.value = {}
                load()
            }

            const openModal = (obj) => {
                console.log("打开模态框")
                specificationPojo.value.specification = JSON.parse(JSON.stringify(obj))
                specificationPojo.value.specificationOptionList = []
                if (JSON.stringify(obj) == '{}') {
                    console.log("新增")
                } else {
                    console.log("修改")
                    axios.get('/mall-manager/specification/querySpecificationOptionBySpecId/' + obj.id).then(rs => {
                        console.log("规格详情：", rs)
                        specificationPojo.value.specificationOptionList = rs.data.data
                        console.log("规格详细数据：", specificationPojo.value)
                    })
                }
                visible.value = true
            }

            const addSpecOption = () =>{
                specificationPojo.value.specificationOptionList.push({})
            }

            const delSpecOption = (index)=>{
                specificationPojo.value.specificationOptionList.splice(index,1)
            }

            const handleOk = () => {
                console.log("新增或编辑数据：",specificationPojo)
                axios.post('/mall-manager/specification/saveOrUpdate',specificationPojo.value).then(rs=>{
                    console.log("新增或编辑结果：",rs)
                    if (rs.data.code == 200){
                        message.success(rs.data.message)
                        visible.value = false
                        load()
                    }else {
                        message.error(rs.data.message)
                    }
                })
            }
            const okDelete = (id) =>{
                axios.delete('/mall-manager/specification/delete/' + id).then(rs=>{
                    console.log("新增或编辑结果：",rs)
                    if (rs.data.code == 200){
                        message.success("删除成功")
                        load()
                    }else {
                        message.error(rs.data.message)
                    }
                })
            }

            onMounted(() => {
                load()
            })

            return {
                columns,
                specification,
                specificationList,
                pagination,
                load,
                searchData,
                handlerChange,
                rest,
                visible,
                openModal,
                handleOk,
                specificationPojo,
                addSpecOption,
                delSpecOption,
                okDelete
            }
        }
    }
</script>

<style scoped>

</style>